﻿using AIVision.Model.EntityModels;
using AIVision.Repository.Services;
using AIVision.UI.ViewModels;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AIVision.UI.Views
{
    /// <summary>
    /// SetCheckTempleteView.xaml 的交互逻辑
    /// </summary>
    public partial class SetCheckTempleteView : UserControl
    {

      
        private readonly CheckTempleteService checkTempleteService;
        private readonly SetCheckTempleteViewModel dataModel;

        public SetCheckTempleteView(CheckTempleteService checkTempleteService)
        {
            InitializeComponent();
            this.checkTempleteService = checkTempleteService;
            dataModel = this.DataContext as SetCheckTempleteViewModel;
            LoadTempleteData();
        }

       private void LoadTempleteData()
        {
            dataModel.CheckTempletes = checkTempleteService.GetAllTempleteList();
        }
        private void LoadStationData()
        {
            var templete = gridCheckTemplete.SelectedItem as CheckTemplateDto;
            if(templete != null)
                dataModel.CheckStations = checkTempleteService.GetStationsByTemplete(templete);
        }
        private void LoadDefectItemData()
        {
            var station =gridCheckStation.SelectedItem as CheckStationDto;
            if (station != null)
                dataModel.DefectItems = checkTempleteService.GetDefectByStation(station);
        }

        

        private void gridCheckTemplete_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            LoadStationData();
        }
        private void gridCheckStation_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            LoadDefectItemData();
        }

        private void gridDefectItems_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }
        private void btnSaveTemplete_Click(object sender, RoutedEventArgs e)
        {
            checkTempleteService.SaveTempletes(dataModel.CheckTempletes);
            LoadTempleteData();
        }

        private void btnSaveStation_Click(object sender, RoutedEventArgs e)
        {
            var templete = gridCheckTemplete.SelectedItem as CheckTemplateDto;
            if(templete !=null && templete.Id>0)
                checkTempleteService.SaveStations(dataModel.CheckStations, templete.Id);
            LoadStationData();
        }
        private void btnSaveDefect_Click(object sender, RoutedEventArgs e)
        {
            var station = gridCheckStation.SelectedItem as CheckStationDto;
            if (station != null && station.Id > 0)
                checkTempleteService.SaveDefects(dataModel.DefectItems, station.Id);
            LoadDefectItemData();
        }
        private void btnTempleteDel_Click(object sender, RoutedEventArgs e)
        {
            var templete = gridCheckTemplete.SelectedItem as CheckTemplateDto;
            if (templete != null && templete.Id > 0)
            {
                checkTempleteService.RemoveTemplete(templete);
                LoadTempleteData();
            }
            else
                dataModel.CheckTempletes.Remove(templete);
        }
        private void btnStationDel_Click(object sender, RoutedEventArgs e)
        {
            var station = gridCheckStation.SelectedItem as CheckStationDto;
            if (station != null && station.Id > 0)
            {
                checkTempleteService.RemoveStation(station);
                LoadStationData();
            }
            else
                dataModel.CheckStations.Remove(station);
        }

        

        private void btnDefectDel_Click(object sender, RoutedEventArgs e)
        {
            var defect = gridDefectItems.SelectedItem as DefectItemDto;
            if (defect != null && defect.Id > 0)
            {
                checkTempleteService.RemoveDefect(defect);
                LoadStationData();
            }
            else
                dataModel.DefectItems.Remove(defect);
        }

        
    }
}
